package cn.cdut.yiwenqian.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;

public class MyFileTool {
	private static String TXTBASEPATH="E:/cdutpostbar/txtfile/";
	private static String IMAGEBASEPATH="E:/cdutpostbar/imagefile/";
	private static String REDIOBASEPATH="E:/cdutpostbar/rediofile/";
	private static String TOOLBASEPATH="E:/cdutpostbar/toolfile/";
	private static String NEWSBASEPATH="E:/cdutpostbar/newsfile/";

	public static String createTxtFile(String content) throws Exception{
		File file=new File(TXTBASEPATH+new Date().getTime()+".txt");
		System.out.println(file.getAbsolutePath());
			if(!file.exists()){
				file.createNewFile();
			}
		if(MyFileTool.writeTextFile(content,file)){
			return file.getAbsolutePath();
		}else{
			return null;
		}
		
	}
	public static String saveImageSourse(File file,String fileName) throws Exception{
		File lfile=new File(IMAGEBASEPATH+new Date().getTime()+"/"+fileName);
		if(MyFileTool.saveFile(file, lfile)){
			return lfile.getAbsolutePath();
		}else{
			return null;
		}
	}
	public static String saveRedioSourse(File file,String fileName) throws Exception{
		File lfile=new File(REDIOBASEPATH+new Date().toString()+fileName);
		if(MyFileTool.saveFile(file, lfile)){
			return lfile.getAbsolutePath();
		}else{
			return null;
		}
	}
	public static String saveToolSourse(File file,String fileName) throws Exception{
		File lfile=new File(TOOLBASEPATH+fileName);
		if(MyFileTool.saveFile(file, lfile)){
			return lfile.getAbsolutePath();
		}else{
			return null;
		}
		
	}
	public static String saveNewsSourse(File file,String fileName) throws Exception{
		File lfile=new File(NEWSBASEPATH+new Date().getTime()+"/"+fileName);
		if(MyFileTool.saveFile(file, lfile)){
			return lfile.getAbsolutePath();
		}else{
			return null;
		}
		
	}
	public static boolean createFile(File file) throws IOException{
		
		if(!file.exists()){
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		
		return true;
	}
	public static boolean saveFile(File ofile,File nfile) throws IOException{
		if(MyFileTool.createFile(nfile)){
			FileInputStream in=new FileInputStream(ofile);
			FileOutputStream out=new FileOutputStream(nfile);
			byte[] buffer=new byte[1024];
			while(in.read(buffer)>0){
				out.write(buffer);
			}
			if(in!=null){
				in.close();
			}
			if(out!=null){
				out.close();
			}
			return true;
		}
		return false;
	}
	
	public static String readTxtFile(File file) throws Exception{
		StringBuffer result=new StringBuffer();
		FileReader freader=null;
		BufferedReader breader=null;
			freader=new FileReader(file);
			breader=new BufferedReader(freader);
				String read=null;
				while((read=breader.readLine())!=null){
					result.append(read+"\r\n");
				}
			if(breader!=null){
				breader.close();
			}
			if(freader!=null){
				freader.close();
			}
		return result.toString();
	}
	public static boolean writeTextFile(String content,File file) throws Exception{
		RandomAccessFile mm=null;
		boolean flag=false;
		FileOutputStream o=null;
		o=new FileOutputStream(file);
		o.write(content.getBytes("UTF-8"));
		o.close();
		flag=true;
		return flag;
	}
	/**
	 * @return the tXTBASEPATH
	 */
	public static String getTXTBASEPATH() {
		return TXTBASEPATH;
	}
	/**
	 * @return the iMAGEBASEPATH
	 */
	public static String getIMAGEBASEPATH() {
		return IMAGEBASEPATH;
	}
	/**
	 * @return the rEDIOBASEPATH
	 */
	public static String getREDIOBASEPATH() {
		return REDIOBASEPATH;
	}
	/**
	 * @return the tOOLBASEPATH
	 */
	public static String getTOOLBASEPATH() {
		return TOOLBASEPATH;
	}

	
}
